package com.ht.modules.system.online.graphreport.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTemplet;
import com.ht.modules.system.online.graphreport.entity.OnlGraphreportTempletItem;
import com.ht.modules.system.online.graphreport.mapper.OnlGraphreportTempletItemMapper;
import com.ht.modules.system.online.graphreport.mapper.OnlGraphreportTempletMapper;
import com.ht.modules.system.online.graphreport.service.IOnlGraphreportTempletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class OnlGraphreportTempletServiceImpl extends ServiceImpl<OnlGraphreportTempletMapper, OnlGraphreportTemplet> implements IOnlGraphreportTempletService {
    @Autowired
    private OnlGraphreportTempletMapper onlGraphreportTempletMapper;
    @Autowired
    private OnlGraphreportTempletItemMapper onlGraphreportTempletItemMapper;
    @Transactional
    public void saveMain(OnlGraphreportTemplet onlGraphreportTemplet, List<OnlGraphreportTempletItem> onlGraphreportTempletItemList) {
        this.onlGraphreportTempletMapper.insert(onlGraphreportTemplet);
        for (OnlGraphreportTempletItem entity : onlGraphreportTempletItemList) {
            entity.setGraphreportTempletId(onlGraphreportTemplet.getId());
            this.onlGraphreportTempletItemMapper.insert(entity);
        }

    }

    @Transactional
    public void updateMain(OnlGraphreportTemplet onlGraphreportTemplet, List<OnlGraphreportTempletItem> onlGraphreportTempletItemList) {
        this.onlGraphreportTempletMapper.updateById(onlGraphreportTemplet);
        this.onlGraphreportTempletItemMapper.deleteByMainId(onlGraphreportTemplet.getId());
        for (OnlGraphreportTempletItem entity : onlGraphreportTempletItemList) {
            entity.setGraphreportTempletId(onlGraphreportTemplet.getId());
            this.onlGraphreportTempletItemMapper.insert(entity);
        }

    }

    @Transactional
    public void delMain(String id) {
        this.onlGraphreportTempletMapper.deleteById(id);
        this.onlGraphreportTempletItemMapper.deleteByMainId(id);
    }

    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            this.onlGraphreportTempletMapper.deleteById(id);
            this.onlGraphreportTempletItemMapper.deleteByMainId(id.toString());
        }
    }
}
